package com.hy.controller;

import com.hy.domain.dto.R;
import com.hy.domain.dto.UserDTO;
import com.hy.service.UserFriendService;
import com.hy.service.UserService;
import com.hy.util.UserHolder;
import com.hy.util.WebsocketUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("im")
public class IMController {
    @Autowired
    private UserFriendService userFriendService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping
    public R<List> onlineUsers() {
        // 获取当前登录用户
        final Long userId = UserHolder.getUser().getUserId();
        // 查询好友列表
        final List<UserDTO> users = userFriendService.listFriendsByUserId(userId);
        final List<Long> collect = users.stream().map(UserDTO::getUserId).collect(Collectors.toList());
        final List list = WebsocketUtil.onlineUserIds(collect);
        return R.ok(list);
    }
}
